/*******************************************************************************
 * Copyright (c) 2011 LegSem.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.txt
 * 
 * Contributors:
 *     LegSem - initial API and implementation
 ******************************************************************************/
package com.legsem.legstar.connector.it.jee6.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.resource.ResourceException;
import javax.resource.cci.Connection;
import javax.resource.cci.ConnectionFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.legsem.legstar.cci.LegStarInteraction;
import com.legsem.legstar.cci.LegStarSingleIOInteractionSpec;
import com.legsem.legstar.cci.LegStarSingleIOInteractionSpecImpl;
import com.legstar.host.invoke.model.HostProgram;
import com.legstar.test.coxb.t1contxt.Dfhcommarea;
import com.legstar.test.coxb.t1contxt.bind.DfhcommareaTransformers;

/**
 * This sample servlet uses the LegStar JCA Connector over sockets directly.
 * <p/>
 * Browser clients invoke this servlet using HTTP GET commands without any
 * parameters.
 * <p/>
 * The target program T1CONTXT just collects a few parameters from the CICS
 * environment.
 */
@WebServlet("/ClientServlet")
public class ClientServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     * The JCA connector as known to JNDI.
     */
    @Resource(name = "legstar.socket.connector")
    private ConnectionFactory connectionFactory;

    /**
     * Execute the remote program and formats the reply data.
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        Connection connection = null;
        try {
            connection = connectionFactory.getConnection();
            LegStarInteraction interaction = (LegStarInteraction) connection
                    .createInteraction();

            Dfhcommarea output = (Dfhcommarea) interaction.execute(getISpec(),
                    new Dfhcommarea());

            PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<head>");
            out.println("<title> A very simple servlet example</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>"
                    + connectionFactory.getMetaData().getAdapterName()
                    + "</h1>");
            out.println("<table border='1'>");
            out.println("<tr>");
            out.println("<td>UserId</td>");
            out.println("<td>" + output.getComUserId() + "</td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>TransactionId</td>");
            out.println("<td>" + output.getComTransactionId() + "</td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>TaskId</td>");
            out.println("<td>" + output.getComTaskId() + "</td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>TermId</td>");
            out.println("<td>" + output.getComTermId() + "</td>");
            out.println("</tr>");
            out.println("</table>");
            out.println("</body>");
            out.println("</html>");
            out.close();
        } catch (ResourceException e) {
            throw new ServletException(e);
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (ResourceException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * Produce an interaction specification with a single input and output
     * structure.
     * <p/>
     * Besides the target mainframe program characteristics, the interaction
     * spec also specifies the LegStar Transformers to be used for input anbd
     * output structures.
     * 
     * @return the interaction spec
     */
    private LegStarSingleIOInteractionSpec getISpec() {
        LegStarSingleIOInteractionSpec ispec = new LegStarSingleIOInteractionSpecImpl();
        HostProgram hostProgram = new HostProgram();
        hostProgram.setName("T1CONTXT");
        hostProgram.setMaxDataLength(24);
        ispec.setHostProgram(hostProgram);
        DfhcommareaTransformers tf = new DfhcommareaTransformers();
        ispec.setInputTransformer(tf.getJavaToHost());
        ispec.setInteractionId("1234");
        ispec.setOutputTransformer(tf.getHostToJava());
        return ispec;

    }
}
